package com.source.lightfowl.basedata.controller;

import com.source.lightfowl.VO.SalesTerminalVO;
import com.source.lightfowl.basedata.entity.SalesTerminal;
import com.source.lightfowl.basedata.service.SalesTerminalService;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 13:19
 */
@Api(value = "销售终端信息管理",tags = "销售终端信息管理")
@RestController
@CrossOrigin
@RequestMapping
public class SalesTerminalController {
    @Autowired
    private SalesTerminalService salesTerminalService;

    //查找全部
    @ApiOperation(value = "查询全部数据")
    @GetMapping("/salesTerminal")
    public MyResult findAll(){
        List<SalesTerminalVO> all = salesTerminalService.findAll();




        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //查找 分页
    @ApiOperation(value = "分页查询（传递两个参数：页数 个数/页）")
    @GetMapping("/salesTerminalFindAllPage")
    public MyResult findAllPage(int page,int pageNum){
        Page<SalesTerminal> all = salesTerminalService.findAllPage(page,pageNum);
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //根据id查
    @ApiOperation(value = "根据id查询")
    @GetMapping("/salesTerminal/id")
    public MyResult findById(Integer id){
        Optional<SalesTerminal> house = salesTerminalService.findById(id);
        if(house!=null)
            return MyResult.build(0,"成功",house);
        else
            return MyResult.build(1,"结果为空");
    }

    //增加 修改
    @ApiOperation(value = "增加/修改 数据 ")
    @PostMapping("/salesTerminal")
    @LogAnnotation(operEvent = "操作类型：添加/修改",operType = 2)
    public MyResult create(SalesTerminal salesTerminal){
       salesTerminalService.save(salesTerminal);

       return MyResult.build(0,"成功");

    }

    @ApiOperation("根据id删除数据")
    @DeleteMapping("/salesTerminal/{id}")
    @LogAnnotation(operEvent = "操作类型：删除记录",operType = 3)
    public MyResult deleteById(@PathVariable Integer id){
        salesTerminalService.deleteById(id);

        return MyResult.build(0,"删除成功");
    }

    //删除 批量
    @ApiOperation(value = "批量删除数据：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/salesTerminalDeleteBatch")
    @LogAnnotation(operEvent = "操作类型：批量删除记录",operType = 4)
    public MyResult deleteBatch(String ids){    //注意：参数的分隔符逗号是英文的
      salesTerminalService.deleteBatch(ids);
        //List<SlaughterHouse> all = slaughterHouseService.findAll();
        return MyResult.build(0,"看是否少了删除的id");

    }

}
